﻿//
// GNS_GameManage.cpp
// GameHall
//
// Created by mr.l on 16/1/27.
//
//

#include "GNS_GameMessage.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_HallScene.h"
#include "GNS_ClientHall.h"


USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;


#define winsize Director::getInstance()->getVisibleSize()

enum
{
	// 系统消息
	MSG_TAG_SYSTEM = 10 ,
	// 私人消息
	MSG_TAG_PRIVATE,
	//分享金币，点卡充值等信息.
	MSG_TAG_GIFT,
	// 勾选框背景
	CELL_TAG_CHECKBOX_BG,
	// 勾选框
	CELL_TAG_CHECKBOX,
	// 已读回执
	CELL_TAG_READRECEIPT,
	// 全选勾选框背景
	BTN_TAG_CHECKBOXALL,
	// 勾选框
	BTN_TAG_CHECKBOX,
	// 删除按钮
	BTN_TAG_DELETE,
	// 邮件昵称
	LABEL_TAG_NICKNAME,
	// 邮件只读正文编辑框
	EDIT_TAG_TEXT,
	MSG_TAG_ACCEPT,
	MSG_TIPS_IS_READ,  //是否已读的标志
};

GNS_GameMessage::GNS_GameMessage()
{

}

GNS_GameMessage::~GNS_GameMessage()
{
    
}

GNS_GameMessage* GNS_GameMessage::createLayer()
{
    auto pGNS_GameManage = GNS_GameMessage::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_GameManage);
    return pGNS_GameManage;
}

bool GNS_GameMessage::init()
{
    if ( !GNS_YJFLayer::init() )
    {
        return false;
    }
   
    return true;
}
void GNS_GameMessage::SetNodeName(std::string CsbName)
{
    m_RootNode = Loadcsb(CsbName);
    addChild(m_RootNode,11);

    auto pBackBtn = static_cast<Button*>(m_RootNode->getChildByName("Button_4"));
	pBackBtn->setTag(BTN_BACK);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::BtnCallBack, this));

	m_SystemTips = static_cast<Sprite*>(m_RootNode->getChildByName("Sprite_1"));
	m_SystemTips->setVisible(false);

	m_GiftTips = static_cast<Sprite*>(m_RootNode->getChildByName("Sprite_3"));
	m_GiftTips->setVisible(false);

	m_FriendTips = static_cast<Sprite*>(m_RootNode->getChildByName("Sprite_2"));
	m_FriendTips->setVisible(false);
	//系统消息面版
	m_SystemMsgPanel = static_cast<Layer*>(m_RootNode->getChildByName("systemPanel"));
	m_systemListView = static_cast<ListView*> (m_SystemMsgPanel->getChildByName("ListView_1"));

	//礼品消息面板
	m_GiftMsgPanel = static_cast<Layer*>(m_RootNode->getChildByName("giftPanel"));
	m_GiftListView = static_cast<ListView*>(m_GiftMsgPanel->getChildByName("ListView_1"));
	
	//好友消息面板
	m_FriendMsgPanel = static_cast<Layer*>(m_RootNode->getChildByName("friendPanel"));
	m_FriendListView = static_cast<ListView*>(m_FriendMsgPanel->getChildByName("ListView_1"));

	auto btnSelect = static_cast<Button*>(m_FriendMsgPanel->getChildByName("Button_22"));
	btnSelect->setTag(BTN_TAG_CHECKBOXALL);
	btnSelect->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::BtnCallBack, this));

	auto *selectall_sign = Sprite::create("circle02_message.png");
	if (selectall_sign)
	{//cell是否全被选择
		selectall_sign->setTag(BTN_TAG_CHECKBOX);
		selectall_sign->setVisible(false);
		selectall_sign->ignoreAnchorPointForPosition(true);
		btnSelect->addChild(selectall_sign);
	}


	m_btnDelete = static_cast<Button*>(m_RootNode->getChildByName("Button_21"));
	m_btnDelete->setTag(BTN_TAG_DELETE);
	m_btnDelete->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::BtnCallBack, this));

	btnSys = static_cast<Button*>(m_RootNode->getChildByName("Button_1"));
	btnSys->setTag(MSG_TAG_SYSTEM);
	btnSys->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::BtnCallBack,this));

	btnPerson = static_cast<Button*>(m_RootNode->getChildByName("Button_1_0"));
	btnPerson->setTag(MSG_TAG_PRIVATE);
	btnPerson->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::BtnCallBack, this));

	btnGift = static_cast<Button*>(m_RootNode->getChildByName("Button_1_1"));
	btnGift->setTag(MSG_TAG_GIFT);
	btnGift->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::BtnCallBack, this));

	ShowPanle(MAIL_SYS);
}

void GNS_GameMessage::ShowPanle(long tag)
{
	switch (tag)
	{
	case MAIL_SYS:
	{
			ShowSystemMsgList();
			m_btnDelete->setVisible(false);
			break;
	}
	case MAIL_FRIEND:
	{		
			showFriendList();
			m_btnDelete->setVisible(true);
			break;
	}
	case MAIL_SHARE_GOLD:
	{	  
			ShowGiftTemMsgList();
			m_btnDelete->setVisible(false);
		   break;
	}
	default:
		break;
	}
	updateUnReadSysMailCount(MAIL_NULL);
}

void GNS_GameMessage::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
		case BTN_BACK:
        {
			Show(false);
            break;
        }
		case MSG_TAG_SYSTEM:    //系统消息
		{
			ShowPanle(MAIL_SYS);
			changeButtonState(MSG_TAG_SYSTEM);
			break;
		}
		case MSG_TAG_PRIVATE:	// 私人消息
		{
			ShowPanle(MAIL_FRIEND);
			changeButtonState(MSG_TAG_PRIVATE);
			break;
		}
		case MSG_TAG_GIFT:	   //礼品消息
		{
			ShowPanle(MAIL_SHARE_GOLD);
			changeButtonState(MSG_TAG_GIFT);
			break;
		}
// 		case MSG_TAG_ACCEPT:   //接收礼品
// 		{
// 			auto par = static_cast<Button*>(pButton->getParent());
// 			int tag = par->getTag();
// 			CMD_Mail iMailInfo = *GNS_PlayerMgr::GetInstance()->FindMail(tag);
// 			// 如果未读
// 			if (iMailInfo.iReadReceipt == 0)
// 			{
// 				CMD_Mail userMail(iMailInfo);
// 				userMail.llRid = iMailInfo.llRid;
// 				userMail.llUid = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
// 				switch (iMailInfo.iMailType)
// 				{
// 					case MAIL_SHARE_GOLD://分享金币
// 					{
// 						   userMail.iType = HAMDLE_MAIL_ACCEPT_GOLD;//领取://接受分享金币或充值 邮件
// 						   break;
// 					}
// 					case MAIL_RECHARGE://点卡充值
// 					{
// 						   userMail.iType = HAMDLE_MAIL_ACCEPT_POINT_CARD;//领取: 充值 邮件
// 						   break;
// 					}
// 					case MAIL_RETURN_EXCHANGE:
// 					{
// 						   userMail.iType = HAMDLE_MAIL_ACCEPT_RETURN_EXCHANGE;//接受 退回兑换金币.
// 						   break;
// 					}
// 					case MAIL_RECHANGE_OR_DEDUCTION: //上线给玩家直接充值，或扣除金币.
// 					{
// 						   userMail.iType = HAMDLE_MAIL_ACCEPT_RECHANGE_OR_DEDUCTION;//接受 上线给玩家直接充值，或扣除金币.
// 						   break;
// 					}
// 			    }
// 
// 			   GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CS_MAIL, (char*)&userMail, sizeof(userMail));
// 
// 			   iMailInfo.iReadReceipt = 1; 
// 			   GNS_PlayerMgr::GetInstance()->UpdateMailToMapList(&iMailInfo); 
// 			   updateUnReadSysMailCount(MAIL_NULL);
// 			}
// 			break;
// 		}
		case BTN_TAG_DELETE:	//删除
		{
			bool bSelected = false;
			int cell_count = m_FriendListView->getChildrenCount();
			for (int i = 0; i < cell_count; i++)
			{
				Button* cell = static_cast<Button*>(m_FriendListView->getItem(i));
				Button* select = static_cast<Button*>(cell->getChildByTag(CELL_TAG_CHECKBOX_BG));
				if (select)
				{
					CCSprite* sign = dynamic_cast<CCSprite*>(select->getChildByTag(CELL_TAG_CHECKBOX));
					if (sign->isVisible())
					{
						bSelected = true;
						long long llRListIndex = (long long)cell->getTag();
						deleteMailByListIndex(llRListIndex);
					}
				}
			}

			if (!bSelected)
			{
				ShowCommonTips("抱歉，请先选择消息");
			}
			break;
		}
			// CELL勾选框
		case CELL_TAG_CHECKBOX_BG:
		{
			CCSprite* sign = dynamic_cast<CCSprite*>(pButton->getChildByTag(CELL_TAG_CHECKBOX));
			if (sign)
			{
				 bool v = sign->isVisible();
				 sign->setVisible(!v);
			}
			 break;
		}
			// 全选勾选框
		case BTN_TAG_CHECKBOXALL:
		{
			CCSprite* sign = dynamic_cast<CCSprite*>(pButton->getChildByTag(BTN_TAG_CHECKBOX));
			if (sign)
			{
				bool v = sign->isVisible();
				sign->setVisible(!v);
				bool v_now = sign->isVisible();
				// 批量设置cell勾选框
				int cell_count = m_FriendListView->getChildrenCount();
				for (int i = 0; i < cell_count; i++)
				{
					Button* cell = static_cast<Button*>(m_FriendListView->getItem(i));
					Button* select = dynamic_cast<Button*>(cell->getChildByTag(CELL_TAG_CHECKBOX_BG));
					if (select)
					{
						CCSprite* sign = dynamic_cast<CCSprite*>(select->getChildByTag(CELL_TAG_CHECKBOX));
						sign->setVisible(v_now);
					}
				}
			}
			break;
		}
            default:
                break;
        }
    }
}

void GNS_GameMessage::deleteMailByListIndex(long long llListIndex)
{
	if (0 >= llListIndex)
		return;
	map<long long, short>::iterator itr = m_MapDeleMailIndex.find(llListIndex);
	if (itr == m_MapDeleMailIndex.end())
	{
		return;
	}

	vector<CMD_Mail> *pVecMail = GNS_PlayerMgr::GetInstance()->FindMail(itr->first, itr->second);
	if (NULL == pVecMail)
	{
		return;
	}

	switch (itr->second)
	{
	case MAIL_SYS: //系统邮件
	{    //系统邮件不能删除
					   break;
	}
	case MAIL_SHARE_GOLD://分享金币
	{
							 break;
	}
	case MAIL_RECHARGE: //点卡充值
	{
							break;
	}
	case MAIL_FRIEND://个人信息
	{
		vector<CMD_Mail>::iterator itr = pVecMail->begin();
		for (; itr != pVecMail->end(); ++itr)
		{
			 // 删除邮件
			 CMD_Mail userMail(*itr);
			 userMail.llRid = (*itr).llRid;
			 userMail.llUid = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
			 userMail.iType = 3;
			 GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CS_MAIL, (char*)&userMail, sizeof(userMail));

		}
		break;
	}
	default:
	{
			   break;
	}
	}
}

void GNS_GameMessage::removeItem(CMD_Mail * pMail)
{
	if (NULL == pMail)
	{
		return;
	}

	if (!m_FriendListView)
		return;

	bool bFind = false;
	for (int i = 0; i < m_FriendListView->getChildrenCount(); i++)
	{
		Button *pCell = static_cast<Button*>(m_FriendListView->getItem(i));
		long long ListIndex = (long long)(pCell->getTag());
		if (ListIndex == GNS_PlayerMgr::GetInstance()->FindListIndex(pMail))
		{
			bFind = true;
			// 删除列表会使用 getMailList 重新绘制列表
			m_FriendListView->removeItem(ListIndex);
			break;
		}
	}
	if (bFind)
	{
		//pSceneHall->UpdateTipsCount();
	}
}

void GNS_GameMessage::itemSysCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
	if (type == ui::Widget::TouchEventType::ENDED) {
		auto pButton = (Button*)pSender;
		int BtnTag = pButton->getTag();
		if (BtnTag>=0)
		{
			map<long long, pair<string, vector<CMD_Mail> > > *pMapSyslistMail = GNS_PlayerMgr::GetInstance()->getMapSysLietMail();
			if (!pMapSyslistMail)
			{
				return;
			}
			map<long long, pair<string, vector<CMD_Mail> > > ::iterator itr = pMapSyslistMail->find(BtnTag);
			if (pMapSyslistMail->end() != itr)
			{
				m_llDeleteRid = BtnTag;
				auto tip = (Sprite*)pButton->getChildByTag(MSG_TIPS_IS_READ);	   //如果找到了 就把未读标志改为已读
				if (tip)
				{
					tip->setVisible(false);
				}
				vector<CMD_Mail>::iterator Vecitr = itr->second.second.begin();
				for (; Vecitr != itr->second.second.end(); ++Vecitr)
				{
					showSystemOrGiftMsg(*Vecitr);
					return;
				}
			}
		}
	}
}

void GNS_GameMessage::itemFriendMsgCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
	if (type == ui::Widget::TouchEventType::ENDED) {
		auto pButton = (Button*)pSender;
		int BtnTag = pButton->getTag();
		if (BtnTag >= 0)
		{
			map<long long, pair<string, vector<CMD_Mail> > > *pMapFriendlistMail = GNS_PlayerMgr::GetInstance()->getMapFriendLietMail();
			if (!pMapFriendlistMail)
			{
				return;
			}
			map<long long, pair<string, vector<CMD_Mail> > > ::iterator itr = pMapFriendlistMail->find(BtnTag);
			if (pMapFriendlistMail->end() != itr)
			{
				auto tip = (Sprite*)pButton->getChildByTag(MSG_TIPS_IS_READ);	   //如果找到了 就把未读标志改为已读
				if (tip)
				{
					tip->setVisible(false);
				}
				m_llDeleteRid = BtnTag;
				showFriendMsg(m_llDeleteRid);
			}
		}
	}
}

void GNS_GameMessage::itemGiftMsgCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
	if (type == ui::Widget::TouchEventType::ENDED) {
		auto pButton = (Button*)pSender;
		int BtnTag = pButton->getTag();
		if (BtnTag >= 0)
		{
			//auto par = static_cast<Button*>(pButton->getParent());
			int tag = pButton->getTag();
			CMD_Mail iMailInfo = *GNS_PlayerMgr::GetInstance()->FindMail(tag);
			// 如果未读
			if (iMailInfo.iReadReceipt == 0)
			{
				CMD_Mail userMail(iMailInfo);
				userMail.llRid = iMailInfo.llRid;
				userMail.llUid = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
				switch (iMailInfo.iMailType)
				{
					case MAIL_SHARE_GOLD://分享金币
					{
						userMail.iType = HAMDLE_MAIL_ACCEPT_GOLD;//领取://接受分享金币或充值 邮件
						break;
					}
					case MAIL_RECHARGE://点卡充值
					{
						userMail.iType = HAMDLE_MAIL_ACCEPT_POINT_CARD;//领取: 充值 邮件
						break;
					}
					case MAIL_RETURN_EXCHANGE:
					{
						userMail.iType = HAMDLE_MAIL_ACCEPT_RETURN_EXCHANGE;//接受 退回兑换金币.
						break;
					}
					case MAIL_RECHANGE_OR_DEDUCTION: //上线给玩家直接充值，或扣除金币.
					{
						userMail.iType = HAMDLE_MAIL_ACCEPT_RECHANGE_OR_DEDUCTION;//接受 上线给玩家直接充值，或扣除金币.
						break;
					}
					case MAIL_DIVIDE_RESULT: //下线贡献的金币 邮件
					{
						userMail.iType =  HAMDLE_MAIL_ACCEPT_DIVIDE_RESULT;//接受 下线贡献的金币 邮件
						break;
					}
				}

				GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CS_MAIL, (char*)&userMail, sizeof(userMail));

				iMailInfo.iReadReceipt = 1;
				GNS_PlayerMgr::GetInstance()->UpdateMailToMapList(&iMailInfo);
				//updateUnReadSysMailCount(MAIL_NULL);  //用MALL_NULL代替礼品消息 因为礼品消息有四种
			}

			map<long long, pair<string, vector<CMD_Mail> > > *pMapShareGoldlistMail = GNS_PlayerMgr::GetInstance()->getMapShareGoldMailList();
			map<long long, pair<string, vector<CMD_Mail> > > *pMapReChargelistMail = GNS_PlayerMgr::GetInstance()->getMapReChargeMailList();
			map<long long, pair<string, vector<CMD_Mail> > > *pMapReturnExchangelistMail = GNS_PlayerMgr::GetInstance()->getMapReturnExchangeGoldMailList();
			map<long long, pair<string, vector<CMD_Mail> > > *pMapDeductionMailList = GNS_PlayerMgr::GetInstance()->getMapDeductionMailList();
			map<long long, pair<string, vector<CMD_Mail> > > *pMapDivideResultMailList = GNS_PlayerMgr::GetInstance()->getMapDivideResultMailList();

			if (pMapShareGoldlistMail)
			{
				map<long long, pair<string, vector<CMD_Mail> > > ::iterator itr = pMapShareGoldlistMail->find(BtnTag);
				if (pMapShareGoldlistMail->end() != itr)
				{
					m_llDeleteRid = BtnTag;
					vector<CMD_Mail>::iterator Vecitr = itr->second.second.begin();
					for (; Vecitr != itr->second.second.end(); ++Vecitr)
					{
						showSystemOrGiftMsg(*Vecitr);
						return;
					}
				}
			}


			if (pMapReChargelistMail)
			{
				map<long long, pair<string, vector<CMD_Mail> > > ::iterator itr = pMapReChargelistMail->find(BtnTag);
				if (pMapReChargelistMail->end() != itr)
				{
					m_llDeleteRid = BtnTag;
					vector<CMD_Mail>::iterator Vecitr = itr->second.second.begin();
					for (; Vecitr != itr->second.second.end(); ++Vecitr)
					{
						showSystemOrGiftMsg(*Vecitr);
						return;
					}
				}
			}

			if (pMapReturnExchangelistMail)
			{
				map<long long, pair<string, vector<CMD_Mail> > > ::iterator itr = pMapReturnExchangelistMail->find(BtnTag);
				if (pMapReturnExchangelistMail->end() != itr)
				{
					m_llDeleteRid = BtnTag;
					vector<CMD_Mail>::iterator Vecitr = itr->second.second.begin();
					for (; Vecitr != itr->second.second.end(); ++Vecitr)
					{
						showSystemOrGiftMsg(*Vecitr);
						return;
					}
				}
			}

			if (pMapDeductionMailList)
			{
				map<long long, pair<string, vector<CMD_Mail> > > ::iterator itr = pMapDeductionMailList->find(BtnTag);
				if (pMapDeductionMailList->end() != itr)
				{
					m_llDeleteRid = BtnTag;
					vector<CMD_Mail>::iterator Vecitr = itr->second.second.begin();
					for (; Vecitr != itr->second.second.end(); ++Vecitr)
					{
						showSystemOrGiftMsg(*Vecitr);
						return;
					}
				}
			}

			if (pMapDivideResultMailList)
			{
				map<long long, pair<string, vector<CMD_Mail> > > ::iterator itr = pMapDivideResultMailList->find(BtnTag);
				if (pMapDivideResultMailList->end() != itr)
				{
					m_llDeleteRid = BtnTag;
					vector<CMD_Mail>::iterator Vecitr = itr->second.second.begin();
					for (; Vecitr != itr->second.second.end(); ++Vecitr)
					{
						showSystemOrGiftMsg(*Vecitr);
						return;
					}
				}
			}


			 updateUnReadSysMailCount(MAIL_NULL);  //用MALL_NULL代替礼品消息 因为礼品消息有四种
		}
	}
}

// 系统邮件细节弹出框
void GNS_GameMessage::showSystemOrGiftMsg(CMD_Mail &pMailInfo)
{
	ShowCommonTips(GTOU(pMailInfo.szText));
	// 如果未读
	if (pMailInfo.iReadReceipt == 0)
	{
		CMD_Mail userMail(pMailInfo);
		userMail.llRid = pMailInfo.llRid;
		userMail.llUid = GNS_PlayerMgr::GetInstance()->getMyUserIdx();
		userMail.iType = 2;
		GNS_ClientHall::getInstance()->sendData(MAIN_MSG_USER, SUB_CS_MAIL, (char*)&userMail, sizeof(userMail));

		pMailInfo.iReadReceipt = 1;

		GNS_PlayerMgr::GetInstance()->UpdateMailToMapList(&pMailInfo);
		updateUnReadSysMailCount(MAIL_NULL);
	}
}

void GNS_GameMessage::showFriendMsg(long long llSelectID)
{
	// 弹个框咯~
	if (!m_FriendMsg)
	{
		m_FriendMsg = GNS_FriendMsg::createLayer("MainScenes/FriendNewsLayer.csb");
		m_FriendMsg->SetNodeName("MainScenes/FriendNewsLayer.csb");
	}

	m_FriendMsg->setSelectPlayerID(llSelectID);
	m_FriendMsg->Show(true);
	updateUnReadSysMailCount(MAIL_FRIEND);
}

void GNS_GameMessage::updateUnReadSysMailCount(int type)
{
	GNS_HallScene* par = (GNS_HallScene*)GNS_SceneMgr::getInstance()->getSceneLayer(LAYER_HALL);
	if (par)
	{
		par->ShowMsgTips();
	}

// 	bool bShow = false;
// 	const int ifullCount = 90;
// 	int iAllCount = GNS_PlayerMgr::GetInstance()->getAllMapListMailcount();
// 	if (iAllCount >= ifullCount)
// 	{
// 		bShow = true;
// 	}
// 	int iCount = 0;
// 	Text* tex = nullptr;
// 	if (type == MAIL_SYS)
// 	{
// 		tex = static_cast<Text*>(m_SystemTips->getChildByName("Text_1"));
// 		m_SystemTips->setVisible(bShow);
// 		iCount = GNS_PlayerMgr::GetInstance()->GetMailCount(type);
// 	}
// 	else if(type == MAIL_FRIEND)
// 	{
// 		tex = static_cast<Text*>(m_FriendTips->getChildByName("Text_1"));
// 		m_FriendTips->setVisible(bShow);
// 		iCount = GNS_PlayerMgr::GetInstance()->GetMailCount(type);
// 	}
// 	else
// 	{
// 		tex = static_cast<Text*>(m_GiftTips->getChildByName("Text_1"));
// 		m_GiftTips->setVisible(bShow);
// 		iCount = GNS_PlayerMgr::GetInstance()->GetMaxGiftMailCount();
// 	}

	updateTip();

// 	if (tex)
// 	{
// 		if (iAllCount >= ifullCount)
// 		{
// 			tex->setString("满");
// 			tex->setVisible(true);
// 		}
// 		else
// 		{
// 			auto str = StringUtils::format("%02d", iCount);
// 			tex->setString(str);
// 			tex->setVisible((iCount > 0) ? true : false);
// 		}
// 	}
}


void GNS_GameMessage::ShowSystemMsgList()
{
	m_systemListView->removeAllItems();
	m_SystemMsgPanel->setVisible(true);
	m_FriendMsgPanel->setVisible(false);
	m_GiftMsgPanel->setVisible(false);
	map<long long, pair<string, vector<CMD_Mail> > > *pMapSyslistMail = pMapSyslistMail = GNS_PlayerMgr::GetInstance()->getMapSysLietMail();
	int ncount = 0;
	map<long long, pair<string, vector<CMD_Mail> > >::reverse_iterator iter = pMapSyslistMail->rbegin();//保存 系统邮件列表 中每列对应的邮件
	for (; iter != pMapSyslistMail->rend(); iter++)
	{
		vector<CMD_Mail>::iterator vitr = iter->second.second.begin();
		if (vitr == iter->second.second.end())
		{
			continue;
		}
		//第一个是最新时间
		CMD_Mail *pMailInfo = &(*vitr);//iter->second.second[0];
		if (NULL == pMailInfo)
		{
			continue;
		}

		char szMailName[USER_NAME_LEN] = { 0 };
		// 每个玩家只显示一条记录 
		long long llNewID = iter->first;
		_snprintf(szMailName, sizeof(szMailName)-1, "%s", iter->second.first.c_str());

		m_MapDeleMailIndex[llNewID] = MAIL_SYS;
		bool bHaveNotRead = pMailInfo->iReadReceipt;

		createSysMsgItem(llNewID, szMailName, pMailInfo, bHaveNotRead);

		if (++ncount >= 80)
		{
			break;
		}
	}
	m_systemListView->setInnerContainerSize(Size(764, 60 * ncount));
}

void GNS_GameMessage::ShowGiftTemMsgList()
{
	m_GiftListView->removeAllItems();
	m_SystemMsgPanel->setVisible(false);
	m_FriendMsgPanel->setVisible(false);
	m_GiftMsgPanel->setVisible(true);
	map<long long, pair<string, vector<CMD_Mail> > > *pMapgiftlistMail = GNS_PlayerMgr::GetInstance()->getMapShareGoldMailList();
	map<long long, pair<string, vector<CMD_Mail> > > *pMapReChargelistMail = GNS_PlayerMgr::GetInstance()->getMapReChargeMailList();
	map<long long, pair<string, vector<CMD_Mail> > > *pMapReturnExchangelistMail = GNS_PlayerMgr::GetInstance()->getMapReturnExchangeGoldMailList();
	map<long long, pair<string, vector<CMD_Mail> > > *pMapDeductionMailList = GNS_PlayerMgr::GetInstance()->getMapDeductionMailList();
	map<long long, pair<string, vector<CMD_Mail> > > *pMapDivideResultMailList = GNS_PlayerMgr::GetInstance()->getMapDivideResultMailList();
	int ncount = 0;
	//先合并分享金币 和 点卡充值的 信息 因为要显示在一起。
	map<long long, pair<string, vector<CMD_Mail> > > tempMap(*pMapgiftlistMail);
	map<long long, pair<string, vector<CMD_Mail> > >::iterator iterTemp = pMapReChargelistMail->begin();
	for (; iterTemp != pMapReChargelistMail->end(); iterTemp++)
	{
		tempMap[iterTemp->first] = iterTemp->second;
	}

	//返还兑奖金币的也要加进来
	iterTemp = pMapReturnExchangelistMail->begin();
	for (; iterTemp != pMapReturnExchangelistMail->end(); iterTemp++)
	{
		tempMap[iterTemp->first] = iterTemp->second;
	}

	iterTemp = pMapDeductionMailList->begin();
	for (; iterTemp != pMapDeductionMailList->end(); iterTemp++)
	{
		tempMap[iterTemp->first] = iterTemp->second;
	}

	iterTemp = pMapDivideResultMailList->begin();
	for (; iterTemp != pMapDivideResultMailList->end(); iterTemp++)
	{
		tempMap[iterTemp->first] = iterTemp->second;
	}

	map<long long, pair<string, vector<CMD_Mail> > >::iterator iter = tempMap.begin();
	for (; iter != tempMap.end(); iter++, ncount++)
	{
		vector<CMD_Mail>::iterator vitr = iter->second.second.begin();
		if (vitr == iter->second.second.end())
		{
			continue;
		}
		//第一个是最新时间
		CMD_Mail *pMailInfo = &(*vitr);//iter->second.second[0];
		if (NULL == pMailInfo)
		{
			continue;
		}

		char szMailName[USER_NAME_LEN] = { 0 };
		// 每个玩家只显示一条记录 
		long long llNewID = iter->first;
		_snprintf(szMailName, sizeof(szMailName)-1, "%s", iter->second.first.c_str());

		m_MapDeleMailIndex[llNewID] = MAIL_SYS;
		bool bHaveNotRead = GNS_PlayerMgr::GetInstance()->IsReadReceiptMail(llNewID, MAIL_SYS);

		createGiftMsgItem(llNewID, szMailName, pMailInfo, bHaveNotRead);

		if (++ncount >= 80)
		{
			break;
		}
	}
	m_GiftListView->setInnerContainerSize(Size(764, 98 * ncount));
}

void GNS_GameMessage::showFriendList()
{
	m_FriendListView->removeAllItems();
	m_SystemMsgPanel->setVisible(false);
	m_FriendMsgPanel->setVisible(true);
	m_GiftMsgPanel->setVisible(false);
	m_MapDeleMailIndex.clear();
	vector<CMD_Mail> *pMailList = GNS_PlayerMgr::GetInstance()->getFriendMailList();
	map<long long, pair<string, vector<CMD_Mail> > > *pMapFriendlistMail = GNS_PlayerMgr::GetInstance()->getMapFriendLietMail();
	if (!pMailList || !pMapFriendlistMail)
	{
		return;
	}

	map<long long, long long> mpMailShor;//list<列表索引,邮件id>按 邮件接收的顺序排序。从最新的往下排.
	vector<long long> vecRidShor;//
	map<long long, pair<string, vector<CMD_Mail> > >::iterator iter = pMapFriendlistMail->begin();
	for (; iter != pMapFriendlistMail->end(); iter++)
	{
		vector<CMD_Mail>::iterator vitr = iter->second.second.begin();
		if (vitr == iter->second.second.end())
		{
			continue;
		}
		//第一个是最新时间
		CMD_Mail *pMailInfo = &(*vitr);//iter->second.second[0];
		if (NULL == pMailInfo)
		{
			continue;
		}

		// 每个玩家只显示一条记录 
		long long llNewID = iter->first;
		vecRidShor.push_back(pMailInfo->llRid);
		mpMailShor[llNewID] = pMailInfo->llRid; 
	}

	sort(vecRidShor.begin(), vecRidShor.end(), greater<long long>());

	vector<long long>::iterator itrVecRids = vecRidShor.begin();//
	for (; itrVecRids != vecRidShor.end(); ++itrVecRids)
	{

		map<long long, long long>::iterator itrMshor2 = mpMailShor.begin();
		for (; itrMshor2 != mpMailShor.end(); ++itrMshor2)
		{
			if (*itrVecRids == itrMshor2->second)
			{
				map<long long, pair<string, vector<CMD_Mail> > >::iterator iter = pMapFriendlistMail->find(itrMshor2->first);
				if (iter != pMapFriendlistMail->end())
				{
					vector<CMD_Mail>::iterator vitr = iter->second.second.begin();
					if (vitr == iter->second.second.end())
					{
						continue;
					}
					//第一个是最新时间
					CMD_Mail *pMailInfo = &(*vitr);//iter->second.second[0];
					if (NULL == pMailInfo)
					{
						continue;
					}

					// 每个玩家只显示一条记录 
					long long llNewID = iter->first;
					m_MapDeleMailIndex[llNewID] = MAIL_FRIEND;
					bool bHaveNotRead = GNS_PlayerMgr::GetInstance()->IsReadReceiptMail(llNewID, MAIL_FRIEND);

					createFriMsgItem(llNewID, (char*)iter->second.first.c_str(), pMailInfo, bHaveNotRead);

				}
				break;
			}
		}
	}
	 
	//map<long long, pair<string, vector<CMD_Mail> > >::iterator iter = pMapFriendlistMail->begin();
	//for (; iter != pMapFriendlistMail->end();iter++)
	//{
	//	vector<CMD_Mail>::iterator vitr = iter->second.second.begin();
	//	if (vitr == iter->second.second.end())
	//	{
	//		continue;
	//	}
	//	//第一个是最新时间
	//	CMD_Mail *pMailInfo = &(*vitr);//iter->second.second[0];
	//	if (NULL == pMailInfo)
	//	{
	//		continue;
	//	}

	//	// 每个玩家只显示一条记录 
	//	long long llNewID = iter->first;
	//	m_MapDeleMailIndex[llNewID] = MAIL_FRIEND;
	//	bool bHaveNotRead = GNS_PlayerMgr::GetInstance()->IsReadReceiptMail(llNewID, MAIL_FRIEND);

	//	createFriMsgItem(llNewID, (char*)iter->second.first.c_str(), pMailInfo, bHaveNotRead);

	//}
	m_FriendListView->setInnerContainerSize(Size(764, 98 * GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_FRIEND)));
}

void GNS_GameMessage::createSysMsgItem(long long llNewID, char* szMailName, CMD_Mail * pMailInfo, bool bHaveNotRead)
{
	if (NULL == pMailInfo)
	{
		return;
	}
	char tmp_buf[150] = { 0 };

	// 背景
	auto *pBg = Button::create("ListViewitem.png", "ListViewitem.png");
	if (pBg)
	{
		pBg->setAnchorPoint(ccp(0, 0));
		pBg->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::itemSysCallBack, this));
		pBg->setTag(llNewID);
		m_systemListView->addChild(pBg);
	}

	if (pMailInfo->iReadReceipt == 0)//是否已读该消息。
	{
		// 勾选框背景
		Sprite *pCheckBoxBg = Sprite::create("l.png");
		pCheckBoxBg->setTag(MSG_TIPS_IS_READ);
		pBg->addChild(pCheckBoxBg);
		pCheckBoxBg->setPosition(ccp(680, pBg->getContentSize().height / 2));
	}

	// 发件人
	Text *pTTFName = Text::create();
	if (pTTFName)
	{
		pTTFName->setColor(ccc3(255, 255, 255));
		pTTFName->setFontSize(20.0f);
		pTTFName->ignoreAnchorPointForPosition(false);
		pTTFName->setAnchorPoint(ccp(0, 0.5f));
		pTTFName->setPosition(ccp(60, (pBg->getContentSize().height) / 2 ));
		//这是系统消息.
		auto str = SetShowText(pMailInfo->szText, "......", 30);
		pTTFName->setString(GTOU(str));
		//pTTFName->setString("繁华的高发司法机构卡死kjgafjsadkffajkgfkhfk恢复健康东方航空的");
		pBg->addChild(pTTFName);
	}

	// 发件时间
	Text *pTTFTime = Text::create();
	if (pTTFTime)
	{
		pTTFTime->setColor(ccc3(255, 255, 255));
		pTTFTime->setFontSize(20.0f);
		pTTFTime->setOpacity(255);
		pTTFTime->ignoreAnchorPointForPosition(false);
		pTTFTime->setAnchorPoint(ccp(0, 0.5f));
		pTTFTime->setPosition(ccp(pTTFName->getPositionX() + pTTFName->getContentSize().width + 20, (pBg->getContentSize().height) / 2 - 10));

// 		SNPRINTF(tmp_buf, "%04d/%02d/%02d   %02d:%02d",
// 			pMailInfo->dtSendTime.iYear,
// 			pMailInfo->dtSendTime.iMon,
// 			pMailInfo->dtSendTime.iDay,
// 			pMailInfo->dtSendTime.iHour,
// 			pMailInfo->dtSendTime.iMin
// 			);
		pTTFTime->setString(GTOU(tmp_buf).c_str());
		pBg->addChild(pTTFTime);
	}

	// 已读回执
	auto *pReadReceipt = Sprite::create("mark_unlook.png");
	if (pReadReceipt)
	{
		pReadReceipt->ignoreAnchorPointForPosition(false);
		pReadReceipt->setAnchorPoint(ccp(0, 0.5));
		pReadReceipt->setPosition(ccp(pTTFTime->getPositionX() + pTTFTime->getContentSize().width + 20, (pBg->getContentSize().height) / 2));
		pReadReceipt->setTag(CELL_TAG_READRECEIPT);
		pReadReceipt->setVisible(bHaveNotRead);
		pBg->addChild(pReadReceipt);
	}
}

void GNS_GameMessage::createGiftMsgItem(long long llNewID, char* szMailName, CMD_Mail * pMailInfo, bool bHaveNotRead)
{
	if (NULL == pMailInfo)
	{
		return;
	}
	char tmp_buf[150] = { 0 };

	// 背景
	auto *pBg = Button::create("ListViewitem.png", "ListViewitem.png");
	if (pBg)
	{
		pBg->setAnchorPoint(ccp(0, 0));
		pBg->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::itemGiftMsgCallBack, this));
		pBg->setTag(llNewID);
		m_GiftListView->addChild(pBg);
	}

	// 发件人
	Text *pTTFName = Text::create();
	if (pTTFName)
	{
		pTTFName->setColor(ccc3(255, 255, 255));
		pTTFName->setFontSize(20.0f);
		pTTFName->ignoreAnchorPointForPosition(false);
		pTTFName->setAnchorPoint(ccp(0, 0.5f));
		pTTFName->setPosition(ccp(60, (pBg->getContentSize().height) / 2));
		//这是系统消息.
		//auto str = SetShowText(pMailInfo->szText, "......", 30);
		pTTFName->setString("礼品消息");
		pBg->addChild(pTTFName);
	}

	// 发件时间
	Text *pTTFTime = Text::create();
	if (pTTFTime)
	{
		pTTFTime->setColor(ccc3(255, 255, 255));
		pTTFTime->setFontSize(20.0f);
		pTTFTime->setOpacity(255);
		pTTFTime->ignoreAnchorPointForPosition(false);
		pTTFTime->setAnchorPoint(ccp(0, 0.5f));
		pTTFTime->setPosition(ccp(pTTFName->getPositionX() + pTTFName->getContentSize().width + 20, (pBg->getContentSize().height) / 2));

		auto tmp_buf = StringUtils::format("%04d/%02d/%02d   %02d:%02d",
			pMailInfo->dtSendTime.year,
			pMailInfo->dtSendTime.mon,
			pMailInfo->dtSendTime.day,
			pMailInfo->dtSendTime.hour,
			pMailInfo->dtSendTime.min);
		pTTFTime->setString(GTOU(tmp_buf.c_str()));
		pBg->addChild(pTTFTime);
	}

	// 已读回执
	auto *pReadReceipt = Sprite::create("mark_unlook.png");
	if (pReadReceipt)
	{
		pReadReceipt->ignoreAnchorPointForPosition(false);
		pReadReceipt->setAnchorPoint(ccp(0, 0.5));
		pReadReceipt->setPosition(ccp(pTTFTime->getPositionX() + pTTFTime->getContentSize().width + 20, (pBg->getContentSize().height) / 2));
		pReadReceipt->setTag(CELL_TAG_READRECEIPT);
		pReadReceipt->setVisible(bHaveNotRead);
		pBg->addChild(pReadReceipt);
	}


	auto btnAc = Button::create("00001.png", "00002.png");
	btnAc->setPosition(Vec2(640, pBg->getContentSize().height / 2));
	btnAc->setTag(pBg->getTag());
	btnAc->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::itemGiftMsgCallBack,this));
	pBg->addChild(btnAc);
}

void GNS_GameMessage::createFriMsgItem(long long llNewID, char* szMailName, CMD_Mail * pMailInfo, bool bHaveNotRead)
{
	if (NULL == pMailInfo)
	{
		return;
	}

	// 背景
	auto *pBg = Button::create("ListViewitem.png", "ListViewitem.png");
	if (pBg)
	{
		pBg->setAnchorPoint(ccp(0, 0));
		pBg->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::itemFriendMsgCallBack, this));
		pBg->setTag(llNewID);
		m_FriendListView->addChild(pBg);
	}

	// 勾选框背景
	Button *pCheckBoxBg = Button::create("circle01_message.png", "circle01_message.png");

	pCheckBoxBg->addTouchEventListener(CC_CALLBACK_2(GNS_GameMessage::BtnCallBack, this));
	pCheckBoxBg->setTag(CELL_TAG_CHECKBOX_BG);

	pCheckBoxBg->ignoreAnchorPointForPosition(false);
	pCheckBoxBg->setAnchorPoint(ccp(0.5, 0.5));
	pBg->addChild(pCheckBoxBg);

	if (pMailInfo->iReadReceipt == 0 && pMailInfo->llTid == GNS_PlayerMgr::GetInstance()->getMyUserIdx())//是否已读该消息。 && pMailInfo->llUid == getSelectPlayerID()
	{
		// 勾选框背景
		Sprite *pCheckBoxBg = Sprite::create("l.png");
		pCheckBoxBg->setTag(MSG_TIPS_IS_READ);
		pBg->addChild(pCheckBoxBg);
		pCheckBoxBg->setPosition(ccp(680, pBg->getContentSize().height / 2));
	}

	// 已勾选
	auto *pCheckBox = Sprite::create("circle02_message.png");
	if (pCheckBox)
	{
		pCheckBox->setTag(CELL_TAG_CHECKBOX);
		pCheckBox->setVisible(false);
		pCheckBox->ignoreAnchorPointForPosition(true);
		pCheckBoxBg->addChild(pCheckBox);
	}
	pCheckBoxBg->setPosition(ccp(20, pBg->getContentSize().height / 2));

	// 发件人
	Text *pTTFName = Text::create();
	if (pTTFName)
	{
		pTTFName->setColor(ccc3(255, 255, 255));
		pTTFName->setFontSize(20.0f);
		pTTFName->ignoreAnchorPointForPosition(false);
		pTTFName->setAnchorPoint(ccp(0, 0.5f));
		pTTFName->setPosition(ccp(60, (pBg->getContentSize().height) / 2));
		//auto str = SetShowText(pMailInfo->szText, "......", 30);
		//pTTFName->setString(GTOU(pMailInfo->szSenderName));
		pTTFName->setString(GTOU(szMailName));
		pBg->addChild(pTTFName);
	}

	// 发件时间
	Text *pTTFTime = Text::create();
	if (pTTFTime)
	{
		pTTFTime->setColor(ccc3(255, 255, 255));
		pTTFTime->setFontSize(20.0f);
		pTTFTime->setOpacity(255);
		pTTFTime->ignoreAnchorPointForPosition(false);
		pTTFTime->setAnchorPoint(ccp(0, 0.5f));
		pTTFTime->setPosition(ccp(pTTFName->getPositionX() + pTTFName->getContentSize().width + 20, (pBg->getContentSize().height) / 2));

		auto tmp_buf = StringUtils::format("%04d/%02d/%02d   %02d:%02d",
					pMailInfo->dtSendTime.year,
					pMailInfo->dtSendTime.mon,
					pMailInfo->dtSendTime.day,
					pMailInfo->dtSendTime.hour,
					pMailInfo->dtSendTime.min
					);
		pTTFTime->setString(GTOU(tmp_buf.c_str()));
		pBg->addChild(pTTFTime);
	}

	// 已读回执
	auto *pReadReceipt = Sprite::create("mark_unlook.png");
	if (pReadReceipt)
	{
		pReadReceipt->ignoreAnchorPointForPosition(false);
		pReadReceipt->setAnchorPoint(ccp(0, 0.5));
		pReadReceipt->setPosition(ccp(pTTFTime->getPositionX() + pTTFTime->getContentSize().width + 20, (pBg->getContentSize().height) / 2));
		pReadReceipt->setTag(CELL_TAG_READRECEIPT);
		pReadReceipt->setVisible(bHaveNotRead);
		pBg->addChild(pReadReceipt);
	}
}

void GNS_GameMessage::updateTip()
{
	if (GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_SYS) > 0)		//有未读系统消息
	{
		m_SystemTips->setVisible(true);
		auto textCount = static_cast<Text*>(m_SystemTips->getChildByName("Text_1"));
		auto str = StringUtils::format("%d", GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_SYS));
		textCount->setString(GTOU(str.c_str()));
	}
	else 
		m_SystemTips->setVisible(false);

	if (GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_FRIEND) > 0)		//有未读个人信息
	{
		m_FriendTips->setVisible(true);
		auto textCount = static_cast<Text*>(m_FriendTips->getChildByName("Text_1"));
		auto str = StringUtils::format("%d", GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_FRIEND));
		textCount->setString(GTOU(str.c_str()));
	}
	else
		m_FriendTips->setVisible(false);

	int nCount = 0;
	nCount += GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_SHARE_GOLD);
	nCount += GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_RECHARGE);
	nCount += GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_RETURN_EXCHANGE);
	nCount += GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_RECHANGE_OR_DEDUCTION);
	nCount += GNS_PlayerMgr::GetInstance()->GetMailCount(MAIL_DIVIDE_RESULT);
	if (nCount > 0)
	{
		m_GiftTips->setVisible(true);
		auto textCount = static_cast<Text*>(m_GiftTips->getChildByName("Text_1"));
		auto str = StringUtils::format("%d", nCount);
		textCount->setString(GTOU(str.c_str()));
	}
	else
		m_GiftTips->setVisible(false);
	return;
}

void GNS_GameMessage::Show(bool isShow)
{
	GNS_YJFLayer::Show(isShow);
	ShowSystemMsgList();
	updateTip();
}

void GNS_GameMessage::setButtonState()
{
	btnSys->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
	btnPerson->setBrightStyle(Widget::BrightStyle::NORMAL);
	btnGift->setBrightStyle(Widget::BrightStyle::NORMAL);
}

void GNS_GameMessage::changeButtonState(int tag)
{
	btnSys->setBrightStyle((tag == btnSys->getTag()) ? Widget::BrightStyle::HIGHLIGHT : Widget::BrightStyle::NORMAL);
	btnPerson->setBrightStyle((tag == btnPerson->getTag()) ? Widget::BrightStyle::HIGHLIGHT : Widget::BrightStyle::NORMAL);
	btnGift->setBrightStyle((tag == btnGift->getTag()) ? Widget::BrightStyle::HIGHLIGHT : Widget::BrightStyle::NORMAL);
}